package hos.retrofit;

import static hos.retrofit.Utils.methodError;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * <p>Title: HttpUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-04-12 21:50
 */
public class RetrofitUtils {

    /**
     * 拼装访问地址
     *
     * @param host 地址
     * @param path 路径
     * @return 访问地址
     */
    public static String url(String host, String path) {
        if (path == null || path.length() == 0) {
            return host;
        }
        if (path.contains("http") || path.contains("https")) {
            return path;
        }
        String hostNow = host;
        String pathNow = path;
        if (host.endsWith("/")) {
            hostNow = host.substring(0, host.length() - 1);
        }
        if (!pathNow.startsWith("/")) {
            pathNow = "/" + pathNow;
        }
        return hostNow + pathNow;
    }


    /**
     * 将Map转换为String
     *
     * @param map 需要转换的目标
     */
    public static String urlParam(Map<String, ?> map) {
        Set<String> queryKeys = map.keySet();
        StringBuilder builder = new StringBuilder();
        for (String queryKey : queryKeys) {
            if (builder.length() != 0) {
                builder.append("&");
            }
            Object value = map.get(queryKey);
            if (value != null) {
                builder.append(queryKey).append("=").append(value);
            } else {
                builder.append(queryKey).append("=");
            }
        }
        return builder.toString();
    }

    public static String parseUrl(String url) {
        if (!url.contains("?")) {
            return url;
        }
        return url.split("\\?")[0];
    }

    /**
     * 将String转换为Map
     *
     * @param url 需要转换的目标
     */
    public static Map<String, Object>parseUrlParam(String url) {
        if (!url.contains("?")) {
            return null;
        }
        String params = url.split("\\?")[1];
        String[] split = params.split("&");
        Map<String, Object> map = new LinkedHashMap<>();
        for (String item : split) {
            String[] itemSplit = item.split("=");
            map.put(itemSplit[0], itemSplit[1]);
        }
        return map;
    }
    public static Map<String,String> parseHeaders(Method method ,String[] headers) {
        Map<String,String> headerMap = new LinkedHashMap<>();
        for (String header : headers) {
            int colon = header.indexOf(':');
            if (colon == -1 || colon == 0 || colon == header.length() - 1) {
                throw methodError(
                        method, "@Headers value must be in the form \"Name: Value\". Found: \"%s\"", header);
            }
            String headerName = header.substring(0, colon);
            String headerValue = header.substring(colon + 1).trim();
            headerMap.put(headerName, headerValue);
        }
        return headerMap;
    }
}
